import { getPORfqs } from "@/lib/procurement-rfqs/services"
import { searchParamsCache } from "@/lib/procurement-rfqs/validations"
import { getValidFilters } from "@/lib/data-table"
import { Shell } from "@/components/shell"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { RFQListTable } from "@/lib/procurement-rfqs/table/rfq-table"
import { type SearchParams } from "@/types/table"
import * as React from "react"

interface RfqPageProps {
  searchParams: Promise<SearchParams>
}

export default async function RfqPage(props: RfqPageProps) {
  // searchParams를 await하여 resolve
  const searchParams = await props.searchParams
  
  // 파라미터 파싱
  const search = searchParamsCache.parse(searchParams);
  const validFilters = getValidFilters(search.filters);
  
  // RFQ 서버는 기본필터와 고급필터를 분리해서 받으므로 그대로 전달
  const promises = Promise.all([
    getPORfqs({
      ...search, // 모든 파라미터 전달 (page, perPage, sort, basicFilters, filters 등)
      filters: validFilters, // 고급 필터를 명시적으로 오버라이드 (파싱된 버전)
    })
  ])

  return (
    <Shell variant="fullscreen" className="h-full"> {/* fullscreen variant 사용 */}
      {/* 고정 헤더 영역 */}
      <div className="flex-shrink-0">
        <div className="flex items-center justify-between">
          <div>
            <h2 className="text-2xl font-bold tracking-tight">
              발주용 견적
            </h2>
          </div>
        </div>
      </div>

      {/* 테이블 영역 - 남은 공간 모두 차지 */}
      <div className="flex-1 min-h-0">
        <React.Suspense
          fallback={
            <DataTableSkeleton
              columnCount={8}
              searchableColumnCount={2}
              filterableColumnCount={3}
              cellWidths={["10rem", "15rem", "12rem", "12rem", "8rem", "8rem", "10rem", "8rem"]}
              shrinkZero
            />
          }
        >
          <RFQListTable promises={promises} className="h-full" />
        </React.Suspense>
      </div>
    </Shell>
  )
}